#include "inet_address.h"
#include "server_socket.h"
#include "socket.h"

#include <iostream>
#include <cstring>

static char resp[] = 
    "HTTP/1.1 200 OK\r\n"
    "Content-Length: 4\r\n"
    "Content-Type: text/plain\r\n"
    "\r\n"
    "pong";


int main() {
#ifdef _WIN32
    WSAData wsa;
    assert(WSAStartup(MAKEWORD(2,2), &wsa) == 0);
#endif

    InetAddress address(nullptr, 8080, false);
    ServerSocket serverSocket(address);
    serverSocket.onAccept = [](Socket* socket){
        // std::cout << "connection made: " << socket->address()->ipPort() << std::endl;
    };
    serverSocket.onMessage = [](Socket* socket, const std::vector<char>& data) {
        socket->write(resp, strlen(resp));
    };
    serverSocket.onClose = [](Socket* socket){
        // std::cout << "connection closed: " << socket->address()->ipPort() << std::endl;
    };
    std::cout << "start listening on " << address.ipPort() << std::endl;
    serverSocket.startListening();
#ifdef _WIN32
    serverSocket.close();
    WSACleanup();
#endif
    return 0;
}